//
//  MBProgressHUDTool.m
//  封装MBProgressHUA
//
//  Created by TakumiShen on 2017/6/6.
//  Copyright © 2017年 Jinzhiqiang. All rights reserved.
//

#import "MBProgressHUDTool.h"
#import "UIImage+GIF.h"
@implementation MBProgressHUDTool
/**
 *  只显示文字HUD
 *
 *  @param string 提示信息
 */
+(void)showMessageMessage:(NSString *)string
{
	[self showMessageForView:nil message:string];
}
+ (void)showHUDWithImageArray:(NSArray *)arr
{
    //自定义view
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].delegate.window];
    //取消背景框
    hud.bezelView.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].delegate.window addSubview:hud];
    UIImageView *images = [[UIImageView alloc]initWithImage:arr[0]];
    images.animationDuration = 2;
    images.animationImages = arr;
    // 开始播放
    [images startAnimating];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    hud.customView = images;
    [hud showAnimated:YES];
}
/**
 *  只显示文字HUD
 *
 *  @param view 哪个页面添加HUD
 *  @param string 提示信息
 */
+(void)showMessageForView:(UIView *)view message:(NSString *)string
{
	if ([[NSThread currentThread] isMainThread] == NO) {
		dispatch_async(dispatch_get_main_queue(), ^{
			[self main_showMessageForView:view message:string];
		});

	}else{
		[self main_showMessageForView:view message:string];
	}

}
+ (void)main_showMessageForView:(UIView *)view message:(NSString *)string
{
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.defaultMotionEffectsEnabled = NO;
    hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
	hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    //hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.4];
	hud.bezelView.color = [UIColor colorWithRed:93.0/255 green:93.0/255 blue:93.0/255 alpha:1.0];
	hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
	//hud.backgroundView.backgroundColor = [UIColor redColor];
    hud.contentColor = [UIColor whiteColor];
    hud.margin = 10.0f;
    hud.label.text = string;
    hud.label.numberOfLines = 0;
	hud.label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
	hud.label.textAlignment = NSTextAlignmentLeft;
	hud.removeFromSuperViewOnHide = YES;
	[hud hideAnimated:YES afterDelay:string.length > 10 ? 3 : 2];
}
/**
 *  显示文字和IconHUD
 *
 *  @param view 哪个页面添加HUD
 *  @param string 提示信息
 *  @param ImageName 图片名
 */
+(void)showSucceedMessageForView:(UIView *)view message:(NSString *)string ImageName:(NSString *)ImageName
{
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    UIImage *image = [[UIImage imageNamed:ImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.label.text = string;
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:1.0];
    hud.contentColor = [UIColor whiteColor];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeCustomView;
    hud.button.hidden = YES;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1];
}

+ (void)showSuccessToast:(NSString *)message
{
    [MBProgressHUDTool showSucceedMessageForView:KeyWindow message:message ImageName:@"upload_suc"];
}
/**
 *  只有菊花HUD
 *
 *  @param view 哪个页面添加HUD
 */
+(void)showChrysanthemumForView:(UIView *)view
{
    [self showChrysanthemumForView:view Message:nil];
}
/**
 *  菊花和文字HUD
 *
 *  @param view 哪个页面添加HUD
 *  @param massege 提示信息
 */
+(void) showChrysanthemumForView:(UIView *)view Message:(NSString *)massege
{
    
    
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }

    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource: @"refresh-gif" ofType: @"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile: filePath];
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage sd_imageWithGIFData:gifData]];
    
    UIView *customView = [UIView new];
    [customView addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(customView.mas_centerX);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    UILabel *titleLable = [UILabel new];
    [customView addSubview:titleLable];
    titleLable.text = massege ?: @"努力加载中";

	// 120 / 12
	BOOL square = titleLable.text.length <= 8;
    titleLable.textColor = JYHexColor(0x9B9B9B);
	titleLable.textAlignment = NSTextAlignmentCenter;
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(customView.mas_centerX);
        make.top.mas_equalTo(imgView.mas_bottom).offset(10);
		make.bottom.mas_offset(0);
		//if (square == NO) {
			make.left.mas_greaterThanOrEqualTo(customView).mas_offset(10);
			make.right.mas_lessThanOrEqualTo(customView).mas_offset(-10);
		//}

    }];
    titleLable.font = [UIFont systemFontOfSize:12];

	HUD.offset = CGPointMake(0, -20);
	HUD.margin = 20.f;
    HUD.customView = customView;
   // customView.backgroundColor = [UIColor redColor];
    
    HUD.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    HUD.bezelView.color = [UIColor whiteColor];
    //文字颜色
    HUD.contentColor = [UIColor whiteColor];
    HUD.animationType = MBProgressHUDAnimationFade;
    
#pragma mark 此处修改菊花颜色
   // HUD.activityIndicatorColor = [UIColor whiteColor];
     [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
    //[UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor redColor];
    //    HUD.label.text = @"123";
#pragma mark 此处修改是方形还是长方形
	HUD.square = square;
#pragma mark 修改文字颜色

    HUD.label.textColor = [UIColor whiteColor];
    
    [view addSubview:HUD];
    [HUD showAnimated:YES];

    
    //    [HUD.label mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(imgView.mas_bottom).offset(0);
    //        make.centerY.mas_equalTo(imgView.mas_centerY);
    //    }];
//    HUD.label.textColor = [UIColor redColor];
    
    HUD.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
}
/**
 *  隐藏HUD
 *
 *  @param view 哪个页面移除HUD
 */
+(void)hideMBProgressHUDForView:(UIView*)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    [MBProgressHUD hideHUDForView:view animated:YES];
}


@end
